/**
 * Created by Administrator on 2016/3/2.
 */

//新闻点击进入layer
gpNamespace.SjpdLayer = cc.Layer.extend({
	contentField:null,
    ctor: function () {
        this._super();

        this.init();
    },
    init: function () {
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdksjpdRes.Pdksjpd_plist);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.sctcRes.Sctc_plist);
        var size = cc.winSize;
        //背景图层
        var background = new cc.Sprite(gpNamespace.sjpd.Sjpd_Bg);
        background.attr({
            x: 202+background.width/2 ,
            y: 72+background.height/2 ,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(background);
       
        //滚动条
        /*var scrollBar = new cc.Sprite(gpNamespace.sjpd.Sjpd_ScrollBar);
        scrollBar.attr({
            x: 1026+scrollBar.width/2 ,
            y: 324+scrollBar.height/2 ,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(scrollBar);*/

        //世界频道消息列表
        var newsList = new tableViewPro.TableView(this, cc.size(760, 250), 0);
        newsList.setPosition(50, 190);
        newsList.setAnchorPoint(0, 0);
        this.newsList = newsList;
        background.addChild(newsList);
        newsList.setTouchEnabled(false);
        newsList.setBarOffset(0, 0);          
        //信息输入
        var Msg = new cc.Sprite(gpNamespace.sjpd.Sjpd_Msg);
        Msg.attr({
            x: 246+Msg.width/2 ,
            y: 182+Msg.height/2 ,
            anchorX: 0.5,
            anchorY: 0.5,
        });
        this.addChild(Msg);
        var contentFieldSize = cc.size(500, 50);
        var contentFieldScale9Sprite = new cc.Scale9Sprite(gpNamespace.help.Bz_Item2InputBg);//，貌似没软用
        var contentField = new cc.EditBox(contentFieldSize, contentFieldScale9Sprite);
        contentField.setPlaceHolder("每条喇叭限50个字");
        contentField.setMaxLength(11);
        contentField.setPlaceholderFontColor(cc.color(150, 150, 150, 255));
        contentField.setFontColor(cc.color(0, 0, 0, 255));
        contentField.setPlaceholderFontSize(22);
        contentField.setFontSize(22);
        contentField.setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE);
        contentField.setInputFlag(cc.EDITBOX_INPUT_FLAG_SENSITIVE);
        contentField.setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);
        contentField.setDelegate(this);
        contentField.attr({
        	x: 80,
        	y: 3,
        	anchorX: 0,
        	anchorY: 0
        });
        Msg.addChild(contentField);
        this.contentField = contentField;

        //发送按钮
        var Send = new cc.MenuItemImage(gpNamespace.sjpd.Sjpd_SendBt, gpNamespace.sjpd.Sjpd_SendBt, this.sendCallbackFunction, this);
        Send.attr({
        	x: 842+Send.width/2 ,
        	y: 178+Send.height/2 
        });
        var sendMenu = new cc.Menu(Send);
        sendMenu.attr({
        	x: 0,
        	y: 0,
        	anchorX: 0,
        	anchorY: 0
        });
        this.addChild(sendMenu);
        gpNamespace.TyNetManager.getInstance().setSjpdLayer(this);
        xinyou.netManager.getHistroyLaba();
    },

    sendCallbackFunction: function (sender) {
    	cc.log("发送喇叭");

    	tyNamespace.setMenuEnable(sender);

    	gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ButtonEffect, false);

    	var content = this.contentField.getString();
    	if (content == ""){
    		tyNamespace.tipsJumper(1,"请勿发送无字天书！");
    		return;
    	}
//    	cc.log(content);
    	xinyou.netManager.usePropBefore(39);
    },
    
    setLabaInfo: function (labaCount) {
    	if (labaCount>0) {
//    		cc.log("使用道具");
    		xinyou.netManager.useProp(39, 0, this.contentField.getString());
    		this.contentField.setString("");
    		this.subOneLaba(false,labaCount);
    	}
    	else {
//  		tyNamespace.tipsJumper(1,"你么有小喇叭，请到商城购买^O^ ！");
    		var content = this.contentField.getString();
    		cc.log("is ok1");
    		var self = this;
    		tyNamespace.tipsJumper(2,"您么有小喇叭,是否花费10钻BB一下?",function(){
    			cc.log("is ok2");
    			if(userInfo.diamond < 10){
    				cc.log("is ok3");
    				tyNamespace.warningJumper(1, tyNamespace.WarningSectencesIndex.NoDiamond)
    			}else{
//  				cc.log("hahah "+content);
    				cc.log("is ok4");
    				xinyou.netManager.useProp(39, 0, content);
    				self.subOneLaba(true);
    				self.contentField.setString("");
    			}
    		});
//    		cc.log("没有道具");
    	}
    },
    
    subOneLaba:function(useDim,count){
    	cc.log("扣喇叭表现");
    	//道具图片
    	var picPath = "#pdk_zjm_008u.png";//喇叭、主界面的素材
    	var num = count-1;
    	if(useDim){
    		picPath = "#zjm_sc_045u.png";//钻石、商城弹窗的素材
    		num = 10;
    	}
    	
    	var labaPic = new cc.Sprite(picPath);
    	if(useDim){
    		labaPic.attr({
    			x:cc.winSize.width/2-60,
    			y:cc.winSize.height/2+15	
    		});
    		labaPic.setScale(0.5, 0.5);
    	}else{
    		labaPic.attr({
    			x:cc.winSize.width/2-60,
    			y:cc.winSize.height/2+15	
    		});
    	}
    	this.addChild(labaPic,10);
    	
    	if(useDim){
    		//减号
    		var sub = new cc.Sprite("#sz_cdd_js_010u.png");
    		sub.attr({
    			x:cc.winSize.width/2-20,
    			y:cc.winSize.height/2+15
    		});
    		this.addChild(sub);
    	}else{
    		//乘号
    		var sub = new cc.Sprite("#sz_cdd_js_012u.png");
    		sub.attr({
    			x:cc.winSize.width/2-20,
    			y:cc.winSize.height/2+15
    		});
    		this.addChild(sub);
    	}
    	
    	//个数
    	var rewards = new tyNamespace.NumLayer(num,18,0,"sz_cdd_js_");
    	rewards.attr({
    		x:cc.winSize.width/2+5,
    		y:cc.winSize.height/2+15
    	});
    	this.addChild(rewards);

    	this.scheduleOnce(function(){
    		rewards.numFadeOut(1);
    		sub.runAction(new cc.FadeOut(1));
    		labaPic.runAction(new cc.FadeOut(1));
    	},1);
    },
    
	setNewsList:function(historyTalk){
	    	
		this.newsList.clearData();
	    this.newsList.addData({
	    	"barInfo": {"width": 2, "height": 40},
	    	"sameItem": [
	    	             {
	    	            	 "dataType": 1,
	    	            	 "headInfo": {
	    	            		 "headType": 0,
	    	            		 "offset": 0,
	    	            		 "imageName": "",
	    	            		 "width": 0
	    	            	 },
	    	            	 "pageInfo": {"colCount": 1},
	    	            	 "cellInfo": {
	    	            		 "cellType": 0,
	    	            		 "cellCount": historyTalk.itemCount,
	    	            		 "imageName": "",
	    	            		 "height": 50
	    	            	 }
	    	             }
	    	             ]
	    });
	    this.newsList.showData();
	    this.newsList.setTouchEnabled(true);
	
	    var cellCount = this.newsList.currentItem.cellInfo.cellCount;
	    var cellType = this.newsList.currentItem.cellInfo.cellType;
	    var currentPage = this.newsList.currentPage;
	    for (var a = 0; a < cellCount; a++) {
	    	var cell;
	    	switch (cellType) {
	    	case 0://sprite
	    		cell = currentPage.getChildByTag(a);
	    		break;
	    		/*case 1://menu
				cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
				break;*/
	    	}
	    	this.setNewsCell(a, cell, historyTalk);
	    }
	    this.newsList.setScrollPosition(historyTalk.itemCount);
	},
	
	setNewsCell:function(index, cell, historyTalk){
		if(historyTalk.talks[index][2].length>30){
			historyTalk.talks[index][2] = historyTalk.talks[index][2].substring(0,30)+"...";
		}
//		cc.log(historyTalk.talks[index][3]+":"+historyTalk.talks[index][2]);
		var sentence = historyTalk.talks[index][3]+":"+historyTalk.talks[index][2];
		var news = new cc.LabelTTF(sentence,gpNamespace.DEFAULTFONT,22);
		if(historyTalk.talks[index][0] == 3){
			news.setFontFillColor(cc.color(108, 28, 11, 255));
		}else{
			news.setFontFillColor(cc.color(80,128,20, 255));
		}
		news.attr({
			x: 0,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		cell.addChild(news);
	}
});